package codeTop;

public class q92 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode preHead = new ListNode(-1);
        preHead.next = head;
        ListNode p = preHead;
        int step = 1;
        while (step != left) {
            p = p.next;
            step++;
        }
        ListNode reverse = reverse(p.next, right - left);
        p.next.next = nextNode;
        p.next = reverse;
        return preHead.next;
    }
    ListNode nextNode;
    public ListNode reverse(ListNode head,int num) {
        ListNode next = head.next;
        int idx = 0;
        while (next != null&&idx<num) {
            ListNode temp = next.next;
            next.next = head;
            head = next;
            next = temp;
            idx++;
        }
        nextNode = next;
        return head;
    }
}
